let app = getApp()
// 获取云数据库引用
const db = wx.cloud.database()
const _ = db.command
const admin = db.collection('adminlist')
const nuc66 = db.collection('nuc__66')
let signUp = []
let signsLogin = []
let _id = ''
let today = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    turename: '',
    index_notice_l: '',
    index_notice_r: '',
    _id: '',
    signUp: [],
    show: false,
    showAction: false,
    showLAction: false,
    showFAction: true,
    popupImg: 'images/popup2.png',
    bannerImg: [],
    lastHotAction: {},
    futureHotAction: {},
    isLogin: false,
    tabbar: {},
    isIphoneX: app.globalData.systemInfo.model.includes('iPhone X')
  },

  toHotAction () {
    wx.navigateTo({ url: '/pages/hotAction/hotAction' })
  },

  onTure () {
    app.globalData.popup = false
    this.setData({ show: false })
  },
  onShow () { this.data.show = app.globalData.popup },

  showAction () { this.setData({ showAction: true }) },
  onClose () {
    this.setData({ showAction: false })
  },

  showLast () {
    this.setData({ showLAction: true, showFAction: false })
  },
  showFuture () {
    this.setData({ showLAction: false, showFAction: true })
  },

  // 预览图片
  previewImage (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: this.data.bannerImg.map((x) => x.src)
    })
  },

  onLoad (options) {
    app.editTabbar()
    const thisMsg = wx.getStorageSync('loginMsg') && JSON.parse(wx.getStorageSync('loginMsg'))
    let that = this
    _id = thisMsg._id
    signsLogin = thisMsg.signUp

    // 获取最新签到数组
    if (_id !== undefined) {
      admin.doc(_id).get({
        success (res) {
          // res.data 包含该记录的数据
          signUp = res.data.signUp
          that.setData({
            signUp: signUp
          })
        }
      })
    }

    // 获取当前年月
    let date = new Date()
    let curYear = date.getFullYear()
    // console.log(curYear)
    let curMonth = date.getMonth() + 1
    // console.log(curMonth)
    let day = date.getDate()
    curMonth = curMonth < 10 ? '0' + curMonth : '' + curMonth
    day = day < 10 ? '0' + day : '' + day
    today = parseInt(curYear + curMonth + day)

    // wx.request({
    //   url: 'http://localhost:3000/swiper',
    //   success: (res) => {
    //     const { statusCode, data } = res
    //     if (statusCode === 200 && data) {
    //       console.log(data)
    //       that.setData({
    //         bannerImg: data
    //       })
    //     }
    //   }
    // })

    // wx.request({
    //   url: 'http://localhost:3000/notice',
    //   success: (res) => {
    //     const { statusCode, data } = res
    //     if (statusCode === 200 && data[0]) {
    //       console.log(data[0])
    //       that.setData({
    //         index_notice_l: data[0].preIndexNotice,
    //         index_notice_r: data[0].nextIndexNotice
    //       })
    //     }
    //   }
    // })

    // 获取通知文字，及轮播图
    nuc66.doc('b00064a760e1c19424ec4dfe4d083fed').get({
      success (res) {
        that.setData({
          index_notice_l: res.data.notice[0],
          index_notice_r: res.data.notice[1],
          bannerImg: res.data.swiper
        })
      }
    })

    // 获取近期活动
    nuc66.doc('b00064a760e2b0c425166dc37ddb33ad').get({
      success (res) {
        that.setData({
          lastHotAction: res.data.lastAction[res.data.lastAction.length - 1],
          futureHotAction: res.data.futureAction[res.data.futureAction.length - 1]
        })
      }
    })

    // 获取用户名字
    let turename = thisMsg.turename

    this.setData({
      turename,
      isLogin: app.globalData.isLogin()
    })

    // 小红点判断
    // this.panduan()
  },

  // 判断这一天是否已经签到
  panduanqiandao () {
    let flagLogin = true
    if (signsLogin !== '' && signsLogin !== undefined) {
      let signsNew = signsLogin[signsLogin.length - 1]
      if (signsNew === today) {
        flagLogin = false
      }
    }
    return flagLogin
  },

  panduan () {
    let that = this
    let flag = that.panduanqiandao()
    if (flag) {
      wx.showTabBarRedDot({
        index: 0
      })
    } else {
      wx.hideTabBarRedDot({
        index: 0
      })
    }
  },

  // 去登录
  toLogin () {
    wx.reLaunch({
      url: '/pages/login/login'
    })
  }

})